//UserFunctions - This class performs all the calls to the web server using the JSONParser class.
//Every function returns a JSONObject of the type {success: , error: , value: }

package com.cah.library;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;


public class UserFunctions
{
	//private static String indexURL = "http://www.theworsteggbert.com";
	//private static String indexURL= "http://192.168.1.12/cahAndroid/"
	//private static String indexURL= "http://192.168.1.6/cahAndroid/"
	//private static String indexURL = "http://192.168.1.8:8080/";
	private static String indexURL = "http://cardsagainstherb.appspot.com";
	private static String authURL = "http://cardsagainstherb.appspot.com/_ah/login?continue=http://cardsagainstherb.appspot.com&auth=";
	
	private JSONParser jsonParser;
	
	  /*Tag List: 
	    * createNewUser
	    * authenticateUser
	    * deauthenticateUser
	    * createGame
	    * cancelGame
	    * addPlayerToGame
	    * removePlayerFromGame
	    * startGame
	    * submitWhite
	    * submitBlack
	    * endGame
	    * fetchCurrentGamesJSON
	    * fetchPendingGamesJSON
	    * fetchPlayerHandlesJSON
	    * fetchGameInfoJSON
	    * fetchSubmissionsJSON
	    * fetchHandJSON
	    * fetchHistoryJSON
	    * fetchPendingGameInfoJSON
	    * fetchRoundHistoryJSON
	    */
	
	private static String CREATE_USER_OR_LOGIN_TAG = "createUserOrLogin";
	private static String GET_USER_KEY_TAG = "getUserKey";
	private static String GET_USERNAME_TAG = "getUsername";
	private static String CREATE_GAME_TAG = "createGame";
	private static String CANCEL_GAME_TAG = "cancelGame";
	private static String ADD_PLAYER_TO_GAME_TAG = "addPlayerToGame";
	private static String REMOVE_PLAYER_FROM_GAME_TAG = "removePlayerFromGame";
	private static String START_GAME_TAG = "startGame";
	private static String SUBMIT_WHITE_TAG = "submitWhite";
	private static String SUBMIT_BLACK_TAG = "submitBlack";
	private static String SET_ALERT_SEEN_TAG = "setAlertToSeen";
	private static String END_GAME_TAG = "endGame";
	private static String FETCH_CURRENT_GAMES_TAG = "fetchCurrentGamesJSON";
	private static String FETCH_PENDING_GAMES_TAG = "fetchPendingGamesJSON";
	private static String FETCH_GAME_INFO_TAG = "fetchGameInfoJSON";
	private static String FETCH_SUBMISSIONS_TAG = "fetchSubmissionsJSON";
	private static String FETCH_HISTORY_TAG = "fetchHistoryJSON";
	private static String FETCH_PENDING_GAME_INFO_TAG = "fetchPendingGameInfoJSON";
	private static String FETCH_ROUND_HISTORY_TAG = "fetchRoundHistoryJSON";
	private static String FETCH_REG_ID_TAG = "fetchRegistrationId";
	private static String REGISTER_DEVICE_TAG = "registerUserDevice";
	private static String FETCH_HISTORY_PAGE_TAG = "fetchHistoryPageJSON";
	
	public static String KEY_SUCCESS = "success";
	public static String KEY_ERROR = "error";
	public static String KEY_VALUE = "value";
	
	private String userKey;
	
	//default constructor
	public UserFunctions()
	{
		this("");
	}
	
	//constructor
	public UserFunctions(String uKey)
	{
		jsonParser = new JSONParser();
		userKey=uKey;
	}
	
	//Creates a new user on the web server or login
	public JSONObject createUserOrLogin(String authToken)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", CREATE_USER_OR_LOGIN_TAG));
		Log.e("authURL", authURL + authToken);
		JSONObject json = jsonParser.getJSONFromAuthUrl(authURL + authToken, params);
		return json;
	}
	
	
	
	
	public String getUid()
	{
		return userKey;
	}
	
	
	//Logs the user in
	public JSONObject getUserKey(String username)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", GET_USER_KEY_TAG));
		params.add(new BasicNameValuePair("username", username));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
			
	//Gets the name associated with a given userID
	public JSONObject getUsername()
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", GET_USERNAME_TAG));
		params.add(new BasicNameValuePair("userKey", userKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Creates a new game with userId as the host
	public JSONObject createGame(String gameName, int maxPlayers)
	{
		String num = ((Integer)maxPlayers).toString();
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", CREATE_GAME_TAG));
		params.add(new BasicNameValuePair("gameName", gameName));
		params.add(new BasicNameValuePair("maxPlayers", num));
		params.add(new BasicNameValuePair("userKey", userKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	
	//Cancels a given game.
	public JSONObject cancelGame(String gameKey)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", CANCEL_GAME_TAG));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		params.add(new BasicNameValuePair("userKey", userKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	
	//Adds a player to a pending game
	public JSONObject addPlayerToGame(String userKey, String gameKey)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", ADD_PLAYER_TO_GAME_TAG));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		params.add(new BasicNameValuePair("userKey", userKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Removes a player from a pending game
	public JSONObject removePlayerFromGame(String userKey, String gameKey)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", REMOVE_PLAYER_FROM_GAME_TAG));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		params.add(new BasicNameValuePair("userKey", userKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Starts a game that is full
	public JSONObject startGame(String gameKey)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", START_GAME_TAG));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		params.add(new BasicNameValuePair("userKey", userKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	
	//Submits a set of white cards for the user and a given game
	public JSONObject submitWhite(int[] whiteCardList, String gameKey)
	{
		
		JSONArray jsonArray = new JSONArray();
		for(int i=0;i<whiteCardList.length;i++)
		{
			jsonArray.put(whiteCardList[i]);
		}
		
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", SUBMIT_WHITE_TAG));
		params.add(new BasicNameValuePair("whiteCards", jsonArray.toString()));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		params.add(new BasicNameValuePair("userKey", userKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Submits a winner for a given game
	public JSONObject submitBlack(String winnerKey, String gameKey)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", SUBMIT_BLACK_TAG));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		params.add(new BasicNameValuePair("userKey", userKey));
		params.add(new BasicNameValuePair("winnerKey", winnerKey));
		Log.e("submitBlack", params.toString());
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Changes an alert to seen
	public JSONObject setAlertToSeen(String gameKey)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", SET_ALERT_SEEN_TAG));
		params.add(new BasicNameValuePair("userKey", userKey));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Ends a game in progress.
	public JSONObject endGame(String gameKey)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", END_GAME_TAG));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Gets all games that the player is currently in
	public JSONObject fetchCurrentGames()
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", FETCH_CURRENT_GAMES_TAG));
		params.add(new BasicNameValuePair("userKey", userKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Gets all games that are currently pending
	public JSONObject fetchPendingGames()
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", FETCH_PENDING_GAMES_TAG));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Gets all the intersecting game info for a given game and user.  See php for details.
	public JSONObject fetchGameInfo(String gameKey)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", FETCH_GAME_INFO_TAG));
		params.add(new BasicNameValuePair("userKey", userKey));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		Log.e("params", params.toString());
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Get all submissions for a given game
	public JSONObject fetchSubmissions(String gameKey)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", FETCH_SUBMISSIONS_TAG));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Get the history for a given game
	public JSONObject fetchHistory(String gameKey)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", FETCH_HISTORY_TAG));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Get the history for a given game
		public JSONObject fetchHistoryPage(String gameKey, int roundEnd)
		{
			List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair("tag", FETCH_HISTORY_PAGE_TAG));
			params.add(new BasicNameValuePair("gameKey", gameKey));
			params.add(new BasicNameValuePair("roundEnd", Integer.toString(roundEnd)));
			JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
			return json;
		}
	
	//Get information about a pending game
	public JSONObject fetchPendingGameInfo(String gameKey)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", FETCH_PENDING_GAME_INFO_TAG));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Get all information for a single completed round in a given game
	public JSONObject fetchRoundHistory(String gameKey, int roundNumber)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", FETCH_ROUND_HISTORY_TAG));
		params.add(new BasicNameValuePair("gameKey", gameKey));
		params.add(new BasicNameValuePair("roundNumber", Integer.toString(roundNumber)));
		Log.e("params", params.toString());
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	//Get the device registration ID for the user
	public JSONObject fetchRegistrationId()
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", FETCH_REG_ID_TAG));
		params.add(new BasicNameValuePair("userKey", userKey));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
	
	public JSONObject registerUserDevice(String regId)
	{
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("tag", REGISTER_DEVICE_TAG));
		params.add(new BasicNameValuePair("userKey", userKey));
		params.add(new BasicNameValuePair("regId", regId));
		JSONObject json = jsonParser.getJSONFromUrl(indexURL, params);
		return json;
	}
}
